//
//  USCustomMenu.m
//  usale0001
//
//  Created by YU on 15/11/16.
//  Copyright © 2015年 YU. All rights reserved.
//

#import "USCustomMenu.h"

#define CellLineEdgeInsets UIEdgeInsetsMake(0, 0, 0, 0)

@interface USCustomMenu()
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat rowHeight;
@end
@implementation USCustomMenu

- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight
{
    self = [super initWithFrame:CGRectMake(0, 0, kUSScreenWidth, kUSScreenHeight)];
    if (self) {
        if (rowHeight <= 0) {
            rowHeight = 40;
        }
        self.backgroundColor = [UIColor clearColor];
        self.origin = origin;
        self.rowHeight = rowHeight;
        self.arrData = [dataArr copy];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + kUSMargin, origin.y + 3, width, rowHeight * dataArr.count) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self addSubview:_tableView];
        _tableView.backgroundColor = kUSColorFromRGB(0x4B8FC2);
        _tableView.alpha = 0.8;
        _tableView.layer.cornerRadius = 2;
        _tableView.bounces = NO;
        _tableView.separatorColor = kUSColorFromRGB(0x62B4F2);
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"USCustomMenu"];
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:CellLineEdgeInsets];
        }
        
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.tableView setLayoutMargins:CellLineEdgeInsets];
        }
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"USCustomMenu"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    if (self.textColor) {
        cell.textLabel.textColor = self.textColor;
    }else{
        
        cell.textLabel.textColor = [UIColor whiteColor];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = self.arrData[indexPath.row];
    if (self.arrImgName.count > indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:self.arrImgName[indexPath.row]];
    }
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    if (self.selectedColor) {
        cell.selectedBackgroundView.backgroundColor = self.selectedColor;
    }else{
        cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];
    }
    cell.imageView.frame = CGRectMake(10, 10, 20, 20);
    cell.textLabel.frame = CGRectMake(40, 10, 100, 20);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if([self.delegate respondsToSelector:@selector(usCustomMenu:didSelectRowAtIndexPath:)]){
        [self.delegate usCustomMenu:tableView didSelectRowAtIndexPath:indexPath];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (!self.NotDissmiss) {
        [self dismissWithCompletion:nil];
        
    }else{
    
        if (self.dismiss) {
            
            self.dismiss();
        }
    }
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:CellLineEdgeInsets];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:CellLineEdgeInsets];
        
    }
}
- (void)dismissWithCompletion:(void (^)(USCustomMenu *object))completion
{
    __weak __typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 0;
        weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x - kUSMargin, weakSelf.origin.y + 3, 0, 0);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        if (completion) {
            completion(weakSelf);
        }
        if (weakSelf.dismiss) {
            weakSelf.dismiss();
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.tableView]) {
        [self dismissWithCompletion:nil];
    }
}

@end
